﻿using System;
using System.Text;
using EzDesk.Apps.Data.ProjectModule;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.Core.Model;
using EzDesk.Utility;
using EzDesk.Utility.Extensions;
using Queryable = System.Linq.Queryable;

namespace EzDesk.Apps.ServiceImpl.ProjectModule.History
{
    /// <summary>
    /// 项目应用了项目模板的项目历史
    /// </summary>
    public class ContentUpdatedProjectHistoryRecorder : ProjectHistoryRecorder
    {
        private Project NewProject = null;

        public ContentUpdatedProjectHistoryRecorder(Project oldProject, Project newProject, Guid senderId)
        {
            Project = oldProject;
            NewProject = newProject;
            initializeHistory(Project, senderId, "ContentUpdatedProjectHistory");
        }

        protected override string GetProjectContent()
        {
            var sb = new StringBuilder();
            if (Project.Name != NewProject.Name)
            {
                sb.AppendLine("{0} 将项目名称修改为 {1}".FormatString(Sender.Name, NewProject.Name));
            }
            if (Project.Code != NewProject.Code)
            {
                sb.AppendLine("{0} 将项目编号修改为 {1}".FormatString(Sender.Name, NewProject.Code));
            }
            if (Project.Description != NewProject.Description)
            {
                sb.AppendLine("{0} 将项目描述修改为 {1}".FormatString(Sender.Name, NewProject.Description));
            }
            var result = sb.ToString().Replace(Environment.NewLine, "<br />");
            return result;
        }
    }

    /// <summary>
    /// 项目中添加了项目计划模板
    /// </summary>
    public class ApplyTempalteProjectHistoryRecorder : ProjectHistoryRecorder
    {
        private TaskTemplate Template { get; set; }

        public ApplyTempalteProjectHistoryRecorder(Project project, Guid? senderId, TaskTemplate template)
        {
            Template = template;
            initializeHistory(project, senderId, "ApplyTempalteProjectHistory");
        }

        protected override string GetProjectContent()
        {
            return "{0} 在项目中添加了 {1} 模板计划".FormatString(Sender.Name, Template.Name);
        }
    }

    /// <summary>
    /// 项目创建的项目历史
    /// </summary>
    public class ChangeStartOnProjectHistoryRecorder : ProjectHistoryRecorder
    {
        public ChangeStartOnProjectHistoryRecorder(Project project, Guid? senderId)
            : base(project, senderId, "ChangeStartOnProjectHistory")
        {
        }

        protected override string GetProjectContent()
        {
            return "{0} 将项目开始时间更新为 {1}".FormatString(Sender.Name, Project.StartOn.Value.ToFullDate());
        }
    }

    /// <summary>
    /// 项目创建的项目历史
    /// </summary>
    public class CreateNewProjectHistoryRecorder : ProjectHistoryRecorder
    {
        public CreateNewProjectHistoryRecorder(Project project, Guid? senderId)
            : base(project, senderId, "CreateNewProjectHistory")
        {
        }

        protected override string GetProjectContent()
        {
            return "{0} 创建了项目".FormatString(Sender.Name);
        }
    }

    /// <summary>
    /// 项目关闭的项目历史
    /// </summary>
    public class CloseProjectHistoryRecorder : ProjectHistoryRecorder
    {
        public CloseProjectHistoryRecorder(Project project, Guid? senderId)
            : base(project, senderId, "CloseProjectHistory")
        {
        }

        protected override string GetProjectContent()
        {
            return "{0} 关闭了项目".FormatString(Sender.Name);
        }
    }

    /// <summary>
    /// 项目重启的项目历史
    /// </summary>
    public class RestartProjectHistoryRecorder : ProjectHistoryRecorder
    {
        public RestartProjectHistoryRecorder(Project project, Guid? senderId)
            : base(project, senderId, "RestartProjectHistory")
        {
        }

        protected override string GetProjectContent()
        {
            return "{0} 重启了项目".FormatString(Sender.Name);
        }
    }

    public abstract class ProjectHistoryRecorder : HistoryRecorderBase
    {
        private ProjectHistory _projectHistory = new ProjectHistory();

        public override ProjectHistory ProjectHistory
        {
            get { return _projectHistory; }
        }

        protected Guid ProjectId { get; set; }
        protected Project Project { get; set; }
        protected Guid? SenderId { get; set; }
        protected SimpleUser Sender { get; set; }

        protected ProjectHistoryRecorder()
        {
        }

        protected ProjectHistoryRecorder(Project project, Guid? senderId, string categoryName)
        {
            initializeHistory(project, senderId, categoryName);
        }

        protected void initializeHistory(Project project, Guid? senderId, string categoryName)
        {
            ProjectId = project.Id;
            Project = project;
            SenderId = senderId;
            if (senderId.HasValue)
            {
                Sender = EzApiClient.GetSimpleUserById(SenderId.Value);    
            }
            
            _projectHistory = new ProjectHistory()
                                  {
                                      Id = Guid.NewGuid(),
                                      CreateOn = DateTime.Now,
                                      HistoryType = HistoryType.Project,
                                      ProjectId = ProjectId,
                                      SenderId = senderId,
                                      LinkUrl = GetProjectLinkUrl(),
                                      Content = GetProjectContent(),
                                      CategoryName = categoryName,
                                  };
        }

        protected abstract string GetProjectContent();
        
        private string GetProjectLinkUrl()
        {
            return "/project/projectHistory/{0}".FormatString(ProjectId.ToString());
        }
    }
}